<script type="text/javascript" src="<?php echo $this->pagePath;?>/js/treeui/jquery.ztree.all-3.4.min.js"></script>
<link rel="stylesheet" type="text/css" href="<?php echo $this->pagePath;?>/js/treeui/zTreeStyle/zTreeStyle.css"/>
<style>
<!--
.top_li{
	float:right !important;
	line-height: 2em;
}
#creat_div , #user_id-label , #is_edit-label , #is_add-label{
	display:none;
}
#activeNodeInGrid{
	background:white;
}
.ztree{
	max-height: 500px;
	overflow: auto;
}
#accordion_menu{
	width:200px;
}
#accordion_menu label{
	border-radius: 3px;
	width: 95%;
	margin: 1px 0px;
}

.press_title_first{
    border-bottom: 1px solid #8b8b8b;
	margin-top: 0 !important;
}
.press_title{
    border-bottom: 1px solid #8b8b8b;
	margin-top: 1em;
	color: #BBB;
}
.press_title span{
    font-size: 1.7em;
    font-weight: bold;
}
-->
</style>

<table width="100%" height="500">
	<tr>
		<td align="left" valign="top" id="menu_tree_td">
			<form id="form_grid_filter" name="form_grid_filter">
			<div id="accordion_menu">
				<h3><a href="#">按业务员筛选</a></h3>
				<div class="">
					<div id="menu_user">
						<input type="radio" checked="checked" id="menu_user_radio0" name="user" value=""/><label for="menu_user_radio0">全部</label>
						<?php foreach ($this->user as $item){?>
						<input type="radio" id="menu_user_radio<?php echo $item->id?>" name="user" value="<?php echo $item->id?>"/><label for="menu_user_radio<?php echo $item->id?>"><?php echo $item->showname?></label>
						<?php }?>
					</div>
				</div>
				<h3><a href="#">客户跟进筛选</a></h3>
				<div class="">
					<div id="menu_press">
						<input type="radio" id="menu_press_radio3" name="menu_press_radio" value=""/><label for="menu_press_radio3">全部</label>					
						<input type="radio" id="menu_press_radio0" name="menu_press_radio" value="0"/><label for="menu_press_radio0">待开发客户(1)</label>
						<input type="radio" id="menu_press_radio1" name="menu_press_radio" value="1"/><label for="menu_press_radio1">跟进中 (12)</label>
						<input type="radio" id="menu_press_radio2" name="menu_press_radio" value="2"/><label for="menu_press_radio2">报价客户(30)</label>
					</div>
				</div>
				<h3><a href="#">按地区筛选</a></h3>
				<div id="menu_tree_1" class="ztree">
					<div id="menu_area">
						<input type="radio" checked="checked" id="menu_area_radio0" name="region" value=""/><label for="menu_area_radio0">全部</label>
						<?php foreach ($this->country as $item){?>
						<input type="radio" id="menu_area_radio<?php echo $item->id?>" name="region" value="<?php echo $item->id?>"/><label for="menu_area_radio<?php echo $item->id?>"><?php echo $item->name_cn?></label>
						<?php }?>
					</div>
				</div>
				<h3><a href="#">按类型筛选</a></h3>
				<div>
					<div id="menu_type">
						<input type="radio" checked="checked" id="menu_type_radio" name="type" value=""/><label for="menu_type_radio">全部</label>					
						<input type="radio" id="menu_type_radio0" name="type" value="0"/><label for="menu_type_radio0">客户</label>
						<input type="radio" id="menu_type_radio2" name="type" value="2"/><label for="menu_type_radio2">供应商</label>
					</div>
				</div>
				<h3><a href="#">按行业筛选</a></h3>
				<div id="menu_tree_2" class="ztree">
					<div id="menu_trade">
						<input type="radio" checked="checked" id="menu_trade_radio0" name="trade" value=""/><label for="menu_trade_radio0">全部</label>										
						<?php foreach ($this->trade as $item){?>
						<input type="radio" id="menu_trade_radio<?php echo $item->id?>" name="trade" value="<?php echo $item->id?>"/><label for="menu_trade_radio<?php echo $item->id?>"><?php echo $item->name_cn?></label>
						<?php }?>
					</div>
				</div>
				<h3><a href="#">按级别筛选</a></h3>
				<div id="menu_tree_3" class="ztree">
					<div id="menu_degree">
						<input type="radio" checked="checked" id="menu_degree_radio0" name="customer_degree" value=""/><label for="menu_degree_radio0">全部</label>																			
						<?php foreach ($this->degree as $item){?>
						<input type="radio" id="menu_degree_radio<?php echo $item->id?>" name="customer_degree" value="<?php echo $item->id?>"/><label for="menu_degree_radio<?php echo $item->id?>"><?php echo $item->name_cn?></label>
						<?php }?>
					</div>
				</div>
			</div>
			<div id="menu_tree" class="ztree">
				<!--THIS IS TREE-->
			</div>
			</form>
		</td>
		<td align="left" valign="top">
			<table id="flex_grid">
				<!--THIS IS GRID-->
			</table>
		</td>
	</tr>
</table>

<script><!--


/**
 * 初始化左侧树
 */
var iniTree = function(){
	//树配置
	var setting = {
		view: {
			//addHoverDom: addHoverDom,
			//removeHoverDom: removeHoverDom,
			selectedMulti: false
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		async: {
			enable: true,
			url:"/menu/items?withRoot=1",
			autoParam:["id", "name=n"],
			dataType:'json',
			otherParam:{"userId":7}//,
			//dataFilter: filter
		},
		callback: {
			//beforeDblClick: zTreeBeforeDblClick
			//beforeClick:refreshGrid
		}
	};
	
	//建立树，
	return $.fn.zTree.init($("#menu_tree"), setting);
};


$(function() {
	//iniTree();
	//客户帅选
	$("#menu_user,#menu_area,#menu_trade,#menu_degree,#menu_type,#menu_press").buttonset().children("input").click(function(){
		var t = $(document.form_grid_filter).serializeArray(),tt={};
		for(var i=0;t[i];i++){var x = t[i];tt[x.name] = x.value}
		window.grids.flexLoad({params:tt});
	});
	$( "#accordion_menu" ).accordion({
		autoHeight: false,
		navigation: true
	})
	window.grids = $("#flex_grid").flexigrid({
		url: '<?php echo $this->pagePath;?>/customer',
		dataType: 'json',
		colModel : [
			{display: 'ID', name : 'id', width : 20, sortable : true, align: 'center'},
			{display: '编号', name : 'code', width : 50, sortable : true, align: 'left'},
			{display: '名称', name : 'name', width : 50, sortable : true, align: 'left'},
			{display: '地区', name : 'region', width : 50, sortable : true, align: 'left'},
			{display: '类型', name : 'type', width : 50, sortable : false, align: 'left'},
			{display: '行业', name : 'trade', width : 50, sortable : false, align: 'left'},
			{display: '级别', name : 'customer_degree', width : 50, sortable : true, align: 'left'},
			{display: '负责人', name : 'user', width : 50, sortable : false, align: 'left'},
			{display: '电话', name : 'tel', width : 80, sortable : true, align: 'left'},
			{display: '邮箱', name : 'email', width : 80, sortable : true, align: 'left'},
			{display: '备用邮箱', name : 'email2', width : 80, sortable : true, align: 'left'},
			{display: 'Skype', name : 'skype', width : 80, sortable : true, align: 'left'},
			{display: '网址', name : 'website', width : 80, sortable : true, align: 'left'},
			{display: '地址', name : 'address', width : 80, sortable : true, align: 'left'},
			{display: '创建时间', name : 'create_time', width : 80, sortable : true, align: 'left'},
			{display: '创建人', name : 'create_user', width : 80, sortable : true, align: 'left'},
			{display: '备注', name : 'note', width : 80, sortable : true, align: 'left', hide: true}
			
		],
		searchitems : [
            {display: '名称' ,name : 'name', isdefault: true},	                     		
			{display: '编号' ,name : 'code', isdefault: true},	                     		
			{display: '邮箱' ,name : 'eamil', isdefault: true}	
		],
		buttons : [
			<?php if($this->allow('add','user','')){?>
			{name: '查看',bclass:"view", onpress : viewInfo},
			<?php } ?>
			<?php if($this->allow('add','user','')){?>
			{name: '新增',bclass:"add", onpress : add},
			<?php } ?>
			<?php if($this->allow('edit','user','')){?>
			{name: '编辑',bclass:"edit", onpress : edit},
			<?php } ?>
			<?php if($this->allow('delete','user','')){?>
			{name: '删除',bclass:"delete", onpress : del},
			<?php } ?>
			/*{name: '编辑权限', onpress : edit_right},*/
			{separator: true}
			<?php if($this->allow('delete','user','')){?>
			,{name: '分配给',bclass:"send", onpress : setUser}
			<?php } ?>
			<?php if($this->allow('delete','user','')){?>
			,{name: '新增跟进计划',bclass:"flag_yes", onpress : addPress}
			<?php } ?>
			<?php if($this->allow('delete','user','')){?>
			,{name: '查看跟进',bclass:"view", onpress : viewPress}
			<?php } ?>
		], 
		sortname: "id",
		sortorder: "asc",
		//singleSelect:true,//是否单选
		usepager: true,
		hideOnSubmit:false,//禁用遮照
		title: '客户列表',
		useRp: true,
		rp: 15,
		showTableToggleBtn: true,
		id:"accountsList",
		width: "99%",
		height: 310,
		onRowClick:function($e){
			bh = $e.find("[abbr='customer_bh'] div").html();
			//element.value = namer;
		},
		onRowDblclick:function($e){
			//双击查看详情
			var id = $e.attr("id").toString().replace("row","") || "0";
			viewInfo(id);
		}
	}).show();
});
var

add = function(name,e){
	var top = $(this).offset().top;
	zendform.zendFormCreat(e,{
		url:"<?php echo $this->pagePath;?>/customer/add",
		dialog:{
			title:"新增客户",
			width:508,
			height:400,
			position:[null,top]
		}
	});
	//重新关联客户邮件
	mailTools.matchcustomer();
}
,edit = function(name,e){
	id = $(e).find(".trSelected")[0].id.toString().replace("row","") || "0";
	var top = $(this).offset().top;
	zendform.zendFormCreat(e,{
		url:"<?php echo $this->pagePath;?>/customer/edit/id/"+(id),
		dialog:{
			title:"修改客户",
			width:508,
			height:400,
			position:[null,top]
		}
	});
	//重新关联客户邮件
	mailTools.matchcustomer();
}
,del = function(name,e){
	var ids = $(e).find(".trSelected");
	var idsData = [];
	$.each(ids,function(i,n){
		var uid = n.id.toString().replace("row","") || "0";
		uid = uid;
		idsData.push(uid);
	});
	if(confirm("您确定要删除这"+ids.length+"项数据吗？")){
		$.ajax({
			"url":"<?php echo $this->pagePath;?>/customer/del",
			"data":{id:idsData},
			"type":"POST",
			"success":function(data){
				if(data.status > 0){
					var randId = "id_" + Date.parse(new Date());
		        	window.grids.flexReload();
		        	$("<div id='"+randId+"'>"+data.msg+"</div>").dialog({title:"SUCCESS!",height: 140,modal: true});
		        	window.setTimeout(function(){$("#"+randId).dialog("close");},1200);
		        }else{
			        $("<div>ERROR!"+data.msg+"</div>").dialog({title:"ERROR!",height: 140,modal: true});
		        }
			},
			"error":function(){
		    	$("<div>ERROR!<br>服务错误！可能是网络忙！</div>").dialog({title:"ERROR!",height: 140,modal: true});
			}
		});
	}
}
//分配负责人
,setUser = function(name,e){
	if($('#flex_grid .trSelected').length > 0){
		var id = $(e).find(".trSelected").attr("id").replace("row","");
		var top = $(this).offset().top;
		zendform.zendFormCreat(e,{
			url:"<?php echo $this->pagePath;?>/customer/setuser/id/"+id,
			dialog:{
				title:"分配责任人",
				width:258,
				height:238,
				position:[null,top]
			}
		});
	}else{
		alert('请选择一项需要编辑的数据。');
	}
}
//查看跟进状态，appendElement为制定附着的element
,viewPress = function(name,e,appendElement){
	id = Number(name) ||  $(e).find(".trSelected")[0].id.toString().replace("row","") || "0";
	//appendElement = $("#"+"cust_pres_"+id);
	if(appendElement){
		id = name;
	}else{
		var top = $(this).offset().top;
		$("<div />",{"id":"cust_pres_"+id,"class":"home_log_list"}).dialog({
			title:"客户当前跟进记录",
			width:508,
			height:400,
			position:[null,top],
			close:function(){$(this).dialog("destroy").remove()}
		});
	}
	appendElement = appendElement ? appendElement : $("#"+"cust_pres_"+id);
	appendElement.html("<img src='<?php echo $this->pagePath;?>/css/flexigrid_images/load.gif'/>");
	$.ajax({
		url:"<?php echo $this->pagePath;?>/msg/listbycustomeranddaliy",
		data:{customer:id},
		dataType:"json",
		success:function(data){
			//今天的
			var htmlstr = '<div class="press_title press_title_first"><span>T</span>oday 今日提醒</div><ul id="home_syslog" class="home_log_list">';
			$.each(data["today"],function(i,d){
				var state = (d.state == 0)?"":"timeDone";
				var title = (d.state == 0)?"标记为已完成":"标记为未完成";
				htmlstr += '<li><a msgid="'+d.id+'" title="'+title+'" class="time timeDoing '+state+'">'+d.alarm_time_f+'</a>'+d.msg+'</li>';
			});
			htmlstr += '</ul>';
			//明天的
			htmlstr += '<div class="press_title"><span>T</span>omorrow 明日提醒</div><ul id="home_syslog" class="home_log_list">';
			$.each(data["tomorrow"],function(i,d){
				var state = (d.state == 0)?"":"timeDone";
				var title = (d.state == 0)?"标记为已完成":"标记为未完成";
				htmlstr += '<li><a msgid="'+d.id+'" title="'+title+'" class="time timeDoing '+state+'">'+d.alarm_time_f+'</a>'+d.msg+'</li>';
			});
			htmlstr += '</ul>';
			//昨天的
			htmlstr += '<div class="press_title"><span>Y</span>estody 昨日提醒</div><ul id="home_syslog" class="home_log_list">';
			$.each(data["yestoday"],function(i,d){
				var state = (d.state == 0)?"":"timeDone";
				var title = (d.state == 0)?"标记为已完成":"标记为未完成";
				htmlstr += '<li><a msgid="'+d.id+'" title="'+title+'" class="time timeDoing '+state+'">'+d.alarm_time_f+'</a>'+d.msg+'</li>';
			});
			htmlstr += '</ul>';
			//之前的
			htmlstr += '<div class="press_title"><span>B</span>efore 之前未完成</div><ul id="home_syslog" class="home_log_list">';
			$.each(data["before"],function(i,d){
				var state = (d.state == 0)?"":"timeDone";
				var title = (d.state == 0)?"标记为已完成":"标记为未完成";
				htmlstr += '<li><a msgid="'+d.id+'" title="'+title+'" class="time timeDoing '+state+'">'+d.alarm_time_f+'</a>'+d.msg+'</li>';
			});			
			htmlstr += '</ul>';
			
			appendElement.html(htmlstr).find("a").click(function(event){
				if(window.confirm("您确定要更改它的完成状态？"))
				//这里的回调参考msgTools.toggleState的实现
				msgTools.toggleState($(this).attr("msgid"),function(data){
					if(data.status){
						if(data.msg){
							var state = (data.msg.state == 0)?"":"timeDone";
							var title = (data.msg.state == 0)?"标记为已完成":"标记为未完成";
							
							$("a[msgid="+data.msg.id+"]").attr("title",title).removeClass("timeDone").addClass(state);
						}						
					}
				})
			});

			//迭代生成昨天、明天、过去全部未完成
			//viewPress(name,e,appendElement)
		}
	});
}
//双击查看详情
,viewInfo = function(id,e){
	id = Number(id) || $(e).find(".trSelected")[0].id.toString().replace("row","") || "0";
	$("<div />",{"id":"cust_info_"+id,"class":"home_log_list"}).html("<img src='<?php echo $this->pagePath;?>/css/flexigrid_images/load.gif'/>").dialog({
		title:"客户",
		width:990,
		height:480,
		position:[null,top],
		close:function(){$(this).dialog("destroy").remove()}
	});
	$("#"+"cust_info_"+id).load("<?php echo $this->pagePath;?>/customer/getiteminfo/id/"+id,function(){
		$(".customer_accordion").accordion({
			collapsible: true
		});
		viewPress(id,e,$("#customer_accordion_press_div"));
	});
}
//新增跟进计划
,addPress = function(name,e){
	if($('#flex_grid .trSelected').length > 0){
		var id = $(e).find(".trSelected").attr("id").replace("row","");
		var top = $(this).offset().top;
		zendform.zendFormCreat(e,{
			url:"<?php echo $this->pagePath;?>/msg/addalarm/customer/"+id,
			dialog:{
				title:"新增跟进计划",
				width:258,
				height:238,
				position:[null,top]
			}
			,afterRenderCreatForm : function(e,arg,Did){
				$("#"+Did+" #alarm_time").datepicker( $.datepicker.regional[ "zh-CN" ] );
			}
		});
	}else{
		alert('请选择一项需要编辑的数据。');
	}
}
;
--></script>
<div id="creat_div"></div>
<div id="edit_div"></div>
<div class="htmlTemplete" id="htmlTemplete" style="display:none">

</div>